{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getFile(path):\n",
    "    files = os.listdir(path)\n",
    "    imgs = []\n",
    "    labels = []\n",
    "    for file in files:\n",
    "        file_path = f'{path}/{file}'\n",
    "        txtArr = np.loadtxt(file_path, dtype=str)\n",
    "        imgs.append(\"\".join(txtArr))\n",
    "        labels.append(file.split('_')[0])\n",
    "    \n",
    "    return pd.DataFrame({\n",
    "        \"imgs\": imgs,\n",
    "        \"labels\": labels\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDFile(paths):\n",
    "    return list(map(getFile, paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "getDFile() takes 1 positional argument but 2 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mg:\\git仓库代码管理\\test\\手写识别系统.ipynb Cell 4'\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/g%3A/git%E4%BB%93%E5%BA%93%E4%BB%A3%E7%A0%81%E7%AE%A1%E7%90%86/test/%E6%89%8B%E5%86%99%E8%AF%86%E5%88%AB%E7%B3%BB%E7%BB%9F.ipynb#ch0000003?line=0'>1</a>\u001b[0m [train, test] \u001b[39m=\u001b[39m getDFile(\u001b[39m\"\u001b[39m\u001b[39mdigits/digits/testDigits\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mdigits/digits/testDigits\u001b[39m\u001b[39m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: getDFile() takes 1 positional argument but 2 were given"
     ]
    }
   ],
   "source": [
    "[train, test] = getDFile(\"digits/digits/testDigits\", \"digits/digits/testDigits\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>imgs</th>\n",
       "      <th>labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0000000000000110000000000000000000000000000011...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000000000000001100000000000000000000000000111...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0000000000010000000000000000000000000000001111...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0000000000000010110000000000000000000000000011...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0000000000000000011100000000000000000000000000...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>941</th>\n",
       "      <td>0000000000000000000000011100000000000000000000...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>942</th>\n",
       "      <td>0000000000000111110000000000000000000000000001...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>943</th>\n",
       "      <td>0000000000111111000000000000000000000000111111...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>944</th>\n",
       "      <td>0000000000111111111000000000000000000000011111...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>945</th>\n",
       "      <td>0000000000000000001111110000000000000000000000...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>946 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  imgs labels\n",
       "0    0000000000000110000000000000000000000000000011...      0\n",
       "1    0000000000000001100000000000000000000000000111...      0\n",
       "2    0000000000010000000000000000000000000000001111...      0\n",
       "3    0000000000000010110000000000000000000000000011...      0\n",
       "4    0000000000000000011100000000000000000000000000...      0\n",
       "..                                                 ...    ...\n",
       "941  0000000000000000000000011100000000000000000000...      9\n",
       "942  0000000000000111110000000000000000000000000001...      9\n",
       "943  0000000000111111000000000000000000000000111111...      9\n",
       "944  0000000000111111111000000000000000000000011111...      9\n",
       "945  0000000000000000001111110000000000000000000000...      9\n",
       "\n",
       "[946 rows x 2 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hamm(str1,str2):\n",
    "    return sum([ a!=b for (a,b) in zip(str1,str2)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hamm(\"123\",\"111\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('1', '1'), ('2', '1'), ('3', '3')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(\"123\",\"113\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def knn(inX, df, k):\n",
    "    dist = df.iloc[:,0].apply(\n",
    "        lambda img: sum([ a!=b for (a,b) in zip(inX,img)])\n",
    "    )\n",
    "\n",
    "    dist_l = pd.DataFrame({\n",
    "        \"dist\": dist,\n",
    "        \"label\": df.iloc[:, -1]\n",
    "    })\n",
    "\n",
    "    dist_k = (dist_l.sort_values(by=\"dist\").iloc[:k])\n",
    "    pred = (dist_k.value_counts(\"label\")).index[0]\n",
    "\n",
    "    return pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "imgs      0000000000000001111000000000000000000000000000...\n",
       "labels                                                    4\n",
       "Name: 399, dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.iloc[399]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn(test.iloc[399,0], train, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def digitsTest(train, test, k):\n",
    "    predict = []\n",
    "\n",
    "\n",
    "    for inX in test.iloc[:,0]:\n",
    "        pred = knn(inX,train,k)\n",
    "        predict.append(pred)\n",
    "\n",
    "    # 预测 和 真实标签 作比较\n",
    "    return np.mean(test.iloc[:,-1] == predict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9873150105708245"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digitsTest(train, test, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>imgs</th>\n",
       "      <th>labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0000000000000110000000000000000000000000000011...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0000000000000001100000000000000000000000000111...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0000000000010000000000000000000000000000001111...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0000000000000010110000000000000000000000000011...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0000000000000000011100000000000000000000000000...</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>941</th>\n",
       "      <td>0000000000000000000000011100000000000000000000...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>942</th>\n",
       "      <td>0000000000000111110000000000000000000000000001...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>943</th>\n",
       "      <td>0000000000111111000000000000000000000000111111...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>944</th>\n",
       "      <td>0000000000111111111000000000000000000000011111...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>945</th>\n",
       "      <td>0000000000000000001111110000000000000000000000...</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>946 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  imgs labels\n",
       "0    0000000000000110000000000000000000000000000011...      0\n",
       "1    0000000000000001100000000000000000000000000111...      0\n",
       "2    0000000000010000000000000000000000000000001111...      0\n",
       "3    0000000000000010110000000000000000000000000011...      0\n",
       "4    0000000000000000011100000000000000000000000000...      0\n",
       "..                                                 ...    ...\n",
       "941  0000000000000000000000011100000000000000000000...      9\n",
       "942  0000000000000111110000000000000000000000000001...      9\n",
       "943  0000000000111111000000000000000000000000111111...      9\n",
       "944  0000000000111111111000000000000000000000011111...      9\n",
       "945  0000000000000000001111110000000000000000000000...      9\n",
       "\n",
       "[946 rows x 2 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "77e92358824cd95e6c4bc8bbd85f8f915bed2cbffdf7b71de1dc818ae11a94c8"
  },
  "kernelspec": {
   "display_name": "Python 3.10.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
